import time
import zlib
import base64
from urllib.parse import urlencode
from Spider.settings import UUID
'''
"areaId=0&cateId=0&cityName=北京&dinnerCountAttrId=&optimusCode=10&originUrl=https://bj.meituan.com/meishi/pn4/&
page=4&partner=126&platform=1&riskLevel=1&sort=&userId=&uuid=71bbe335a74f4f438b05.1576821600.1.0.0"

['areaId=0', 'cateId=0', 'cityName=北京', 'dinnerCountAttrId=', 'optimusCode=10', 
'originUrl=https://bj.meituan.com/meishi/pn4/', 'page=4', 'partner=126', 'platform=1', 
'riskLevel=1', 'sort=', 'userId=', 'uuid=71bbe335a74f4f438b05.1576821600.1.0.0']
'''


class GetPort(object):
    def __init__(self, city, city_english, page):
        self.city = city
        self.page = page
        self.city_english = city_english
        self.uuid = UUID

    def sign(self):
        first_dic = {
            'areaId': 0,
            'cateId': 0,
            'cityName': self.city,
            'dinnerCountAttrId': '',
            'optimusCode': 10,
            'originUrl': f'https://{self.city_english}.meituan.com/meishi/pn{self.page}/',
            'page': self.page,
            'partner': 126,
            'platform': 1,
            'riskLevel': 1,
            'sort': '',
            'userId': '',
            'uuid': self.uuid,
        }
        lis = []
        for k, v in first_dic.items():
            o = k + '=' + str(v)
            lis.append(o)
        first_str = '&'.join(lis)
        bs = first_str.encode()
        zl = zlib.compress(bs)
        bs64 = base64.b64encode(zl)
        sign = str(bs64, encoding='utf-8')
        return sign

    def token(self):
        ts = int(time.time() * 1000)
        bI = [f'https://{self.city_english}.meituan.com/meishi/pn{self.page}',
              f'https://{self.city_english}.meituan.com/meishi/']
        new_dic = {
            'rId': 100900,
            'ver': '1.0.6',
            'ts': ts,
            'cts': ts + 130,
            'brVD': [1014, 754],
            'brR': [[1536, 864], [1536, 824], 24, 24],
            'bI': bI,
            'mT': [],
            'kT': [],
            'aT': [],
            'tT': [],
            'aM': '',
            'sign': self.sign()
         }
        str_dic = str(new_dic)
        bs = str_dic.encode()
        zb = zlib.compress(bs)
        bs64 = base64.b64encode(zb)
        token = str(bs64, encoding='utf-8')
        return token

    @property
    def get_url(self):
        first_dic = {
            'cityName': self.city,
            'cateId': 0,
            'areaId': 0,
            'sort': '',
            'dinnerCountAttrId': '',
            'page': self.page,
            'userId': 2835371001,
            'uuid': self.uuid,
            'platform': 1,
            'partner': 126,
            'originUrl': f'https://{self.city_english}.meituan.com/meishi/',
            'riskLevel': 1,
            'optimusCode': 10,
            '_token': self.token()
        }

        args_url = urlencode(first_dic)
        full_url = f'https://{self.city_english}.meituan.com/meishi/api/poi/getPoiList?' + args_url
        return full_url


if __name__ == '__main__':

    a = GetPort('北京', 'bj', 1)
    print(a.get_url)